package com.songlanyun.modules.aridroprecord.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.modules.account.entity.UserVerifiedEntity;
import com.songlanyun.modules.account.service.UserVerifiedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.aridroprecord.dao.AridropRecordDao;
import com.songlanyun.modules.aridroprecord.entity.AridropRecordEntity;
import com.songlanyun.modules.aridroprecord.service.AridropRecordService;


@Service("aridropRecordService")
public class AridropRecordServiceImpl extends ServiceImpl<AridropRecordDao, AridropRecordEntity> implements AridropRecordService {


    @Autowired
    private UserVerifiedService userVerifiedService;

    @Override
    public AridropRecordEntity create(AridropRecordEntity entity) {
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public AridropRecordEntity modify(AridropRecordEntity entity) {
        return null;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<AridropRecordEntity> wrapper = new LambdaQueryWrapper<>();
        Date startTime = MapUtil.getDate(params, "start_time");
        if(ObjectUtil.isNotNull(startTime))
        {
            wrapper.gt(AridropRecordEntity::getCreateTime,startTime);
        }

        Date endTime = MapUtil.getDate(params,"end_time");
        if(ObjectUtil.isNotNull(endTime))
        {
            wrapper.lt(AridropRecordEntity::getCreateTime,endTime);
        }

        wrapper.orderByDesc(AridropRecordEntity::getCreateTime);

        IPage<AridropRecordEntity> page = this.page(
                new Query<AridropRecordEntity>().getPage(params),
                wrapper
        );

        page.getRecords().forEach(o->{
            UserVerifiedEntity userVerifiedEntity = userVerifiedService.loadById(o.getUserId(), false);
            if(ObjectUtil.isNotNull(userVerifiedEntity) && o.getRealName().equals("未实名"))
            {
                o.setRealName(userVerifiedEntity.getRealName());
            }
        });
        return new PageUtils(page);
    }

}
